using System;
using System.Collections.Generic;
using System.Text;

namespace CodePlex.Resourceful
{
    public static class MimeMap
    {

        private static readonly NameValuePairs _map;

        static MimeMap()
        {
            _map = new NameValuePairs(StringComparer.CurrentCultureIgnoreCase);

            _map[".323"] = MediaType.TextH323;
            _map[".asx"] = MediaType.VideoExMsAsf;
            _map[".acx"] = MediaType.ApplicationInternetPropertyStream;
            _map[".ai"] = MediaType.ApplicationPostscript;
            _map[".aif"] = MediaType.AudioExAiff;
            _map[".aiff"] = MediaType.AudioAiff;
            _map[".axs"] = MediaType.ApplicationOlescript;
            _map[".aifc"] = MediaType.AudioAiff;
            _map[".asr"] = MediaType.VideoExMsAsf;
            _map[".avi"] = MediaType.VideoExMsVideo;
            _map[".asf"] = MediaType.VideoExMsAsf;
            _map[".au"] = MediaType.AudioBasic;
            _map[".application"] = MediaType.ApplicationExMsApplication;
            _map[".bin"] = MediaType.ApplicationOctetStream;
            _map[".bas"] = MediaType.TextPlain;
            _map[".bcpio"] = MediaType.ApplicationExBcpio;
            _map[".bmp"] = MediaType.ImageBmp;
            _map[".cdf"] = MediaType.ApplicationExCdf;
            _map[".cat"] = MediaType.ApplicationVndmsPkiseccat;
            _map[".crt"] = MediaType.ApplicationExX509CaCert;
            _map[".c"] = MediaType.TextPlain;
            _map[".css"] = MediaType.TextCss;
            _map[".cer"] = MediaType.ApplicationExX509CaCert;
            _map[".crl"] = MediaType.ApplicationPkixCrl;
            _map[".cmx"] = MediaType.ImageExCmx;
            _map[".csh"] = MediaType.ApplicationExCsh;
            _map[".cod"] = MediaType.ImageCisCod;
            _map[".cpio"] = MediaType.ApplicationExCpio;
            _map[".clp"] = MediaType.ApplicationExMsclip;
            _map[".crd"] = MediaType.ApplicationExMscardfile;
            _map[".deploy"] = MediaType.ApplicationOctetStream;
            _map[".dll"] = MediaType.ApplicationExMsdownload;
            _map[".dot"] = MediaType.ApplicationMsword;
            _map[".doc"] = MediaType.ApplicationMsword;
            _map[".dvi"] = MediaType.ApplicationExDvi;
            _map[".dir"] = MediaType.ApplicationExDirector;
            _map[".dxr"] = MediaType.ApplicationExDirector;
            _map[".der"] = MediaType.ApplicationExX509CaCert;
            _map[".dib"] = MediaType.ImageBmp;
            _map[".dcr"] = MediaType.ApplicationExDirector;
            _map[".disco"] = MediaType.TextXml;
            _map[".exe"] = MediaType.ApplicationOctetStream;
            _map[".etx"] = MediaType.TextExSetext;
            _map[".evy"] = MediaType.ApplicationEnvoy;
            _map[".eml"] = MediaType.MessageRfc822;
            _map[".eps"] = MediaType.ApplicationPostscript;
            _map[".flr"] = MediaType.ExWorldExVrml;
            _map[".fif"] = MediaType.ApplicationFractals;
            _map[".gtar"] = MediaType.ApplicationExGtar;
            _map[".gif"] = MediaType.ImageGif;
            _map[".gz"] = MediaType.ApplicationExGzip;
            _map[".hta"] = MediaType.ApplicationHta;
            _map[".htc"] = MediaType.TextExComponent;
            _map[".htt"] = MediaType.TextWebviewhtml;
            _map[".h"] = MediaType.TextPlain;
            _map[".hdf"] = MediaType.ApplicationExHdf;
            _map[".hlp"] = MediaType.ApplicationWinhlp;
            _map[".html"] = MediaType.TextHtml;
            _map[".htm"] = MediaType.TextHtml;
            _map[".hqx"] = MediaType.ApplicationMacBinhex40;
            _map[".isp"] = MediaType.ApplicationExInternetSignup;
            _map[".iii"] = MediaType.ApplicationExIphone;
            _map[".ief"] = MediaType.ImageIef;
            _map[".ivf"] = MediaType.VideoExIvf;
            _map[".ins"] = MediaType.ApplicationExInternetSignup;
            _map[".ico"] = MediaType.ImageExIcon;
            _map[".jpg"] = MediaType.ImageJpeg;
            _map[".jfif"] = MediaType.ImagePjpeg;
            _map[".jpe"] = MediaType.ImageJpeg;
            _map[".jpeg"] = MediaType.ImageJpeg;
            _map[".js"] = MediaType.ApplicationExJavascript;
            _map[".lsx"] = MediaType.VideoExLaAsf;
            _map[".latex"] = MediaType.ApplicationExLatex;
            _map[".lsf"] = MediaType.VideoExLaAsf;
            _map[".manifest"] = MediaType.ApplicationExMsManifest;
            _map[".mhtml"] = MediaType.MessageRfc822;
            _map[".mny"] = MediaType.ApplicationExMsmoney;
            _map[".mht"] = MediaType.MessageRfc822;
            _map[".mid"] = MediaType.AudioMid;
            _map[".mpv2"] = MediaType.VideoMpeg;
            _map[".man"] = MediaType.ApplicationExTroffMan;
            _map[".mvb"] = MediaType.ApplicationExMsmediaview;
            _map[".mpeg"] = MediaType.VideoMpeg;
            _map[".m3u"] = MediaType.AudioExMpegurl;
            _map[".mdb"] = MediaType.ApplicationExMsaccess;
            _map[".mpp"] = MediaType.ApplicationVndMsProject;
            _map[".m1v"] = MediaType.VideoMpeg;
            _map[".mpa"] = MediaType.VideoMpeg;
            _map[".me"] = MediaType.ApplicationExTroffMe;
            _map[".m13"] = MediaType.ApplicationExMsmediaview;
            _map[".movie"] = MediaType.VideoExSgiMovie;
            _map[".m14"] = MediaType.ApplicationExMsmediaview;
            _map[".mpe"] = MediaType.VideoMpeg;
            _map[".mp2"] = MediaType.VideoMpeg;
            _map[".mov"] = MediaType.VideoQuicktime;
            _map[".mp3"] = MediaType.AudioMpeg;
            _map[".mpg"] = MediaType.VideoMpeg;
            _map[".ms"] = MediaType.ApplicationExTroffMs;
            _map[".nc"] = MediaType.ApplicationExNetcdf;
            _map[".nws"] = MediaType.MessageRfc822;
            _map[".oda"] = MediaType.ApplicationOda;
            _map[".ods"] = MediaType.ApplicationOleobject;
            _map[".pmc"] = MediaType.ApplicationExPerfmon;
            _map[".p7r"] = MediaType.ApplicationExPkcs7Certreqresp;
            _map[".p7b"] = MediaType.ApplicationExPkcs7Certificates;
            _map[".p7s"] = MediaType.ApplicationPkcs7Signature;
            _map[".pmw"] = MediaType.ApplicationExPerfmon;
            _map[".ps"] = MediaType.ApplicationPostscript;
            _map[".p7c"] = MediaType.ApplicationPkcs7Mime;
            _map[".pbm"] = MediaType.ImageExPortableBitmap;
            _map[".ppm"] = MediaType.ImageExPortablePixmap;
            _map[".pub"] = MediaType.ApplicationExMspublisher;
            _map[".pnm"] = MediaType.ImageExPortableAnymap;
            _map[".pml"] = MediaType.ApplicationExPerfmon;
            _map[".p10"] = MediaType.ApplicationPkcs10;
            _map[".pfx"] = MediaType.ApplicationExPkcs12;
            _map[".p12"] = MediaType.ApplicationExPkcs12;
            _map[".pdf"] = MediaType.ApplicationPdf;
            _map[".pps"] = MediaType.ApplicationVndMsPowerpoint;
            _map[".p7m"] = MediaType.ApplicationPkcs7Mime;
            _map[".pko"] = MediaType.ApplicationVndmsPkipko;
            _map[".ppt"] = MediaType.ApplicationVndMsPowerpoint;
            _map[".pmr"] = MediaType.ApplicationExPerfmon;
            _map[".pma"] = MediaType.ApplicationExPerfmon;
            _map[".png"] = MediaType.ImagePng;
            _map[".pot"] = MediaType.ApplicationVndMsPowerpoint;
            _map[".prf"] = MediaType.ApplicationPicsRules;
            _map[".pgm"] = MediaType.ImageExPortableGraymap;
            _map[".qt"] = MediaType.VideoQuicktime;
            _map[".ra"] = MediaType.AudioExPnRealaudio;
            _map[".rgb"] = MediaType.ImageExRgb;
            _map[".ram"] = MediaType.AudioExPnRealaudio;
            _map[".rmi"] = MediaType.AudioMid;
            _map[".ras"] = MediaType.ImageExCmuRaster;
            _map[".roff"] = MediaType.ApplicationExTroff;
            _map[".rtf"] = MediaType.ApplicationRtf;
            _map[".rtx"] = MediaType.TextRichtext;
            _map[".sv4crc"] = MediaType.ApplicationExSv4crc;
            _map[".spc"] = MediaType.ApplicationExPkcs7Certificates;
            _map[".setreg"] = MediaType.ApplicationSetRegistrationInitiation;
            _map[".snd"] = MediaType.AudioBasic;
            _map[".stl"] = MediaType.ApplicationVndmsPkistl;
            _map[".setpay"] = MediaType.ApplicationSetPaymentInitiation;
            _map[".stm"] = MediaType.TextHtml;
            _map[".shar"] = MediaType.ApplicationExShar;
            _map[".sh"] = MediaType.ApplicationExSh;
            _map[".sit"] = MediaType.ApplicationExStuffit;
            _map[".spl"] = MediaType.ApplicationFuturesplash;
            _map[".sct"] = MediaType.TextScriptlet;
            _map[".scd"] = MediaType.ApplicationExMsschedule;
            _map[".sst"] = MediaType.ApplicationVndmsPkicertstore;
            _map[".src"] = MediaType.ApplicationExWaisSource;
            _map[".sv4cpio"] = MediaType.ApplicationExSv4cpio;
            _map[".tex"] = MediaType.ApplicationExTex;
            _map[".tgz"] = MediaType.ApplicationExCompressed;
            _map[".t"] = MediaType.ApplicationExTroff;
            _map[".tar"] = MediaType.ApplicationExTar;
            _map[".tr"] = MediaType.ApplicationExTroff;
            _map[".tif"] = MediaType.ImageTiff;
            _map[".txt"] = MediaType.TextPlain;
            _map[".texinfo"] = MediaType.ApplicationExTexinfo;
            _map[".trm"] = MediaType.ApplicationExMsterminal;
            _map[".tiff"] = MediaType.ImageTiff;
            _map[".tcl"] = MediaType.ApplicationExTcl;
            _map[".texi"] = MediaType.ApplicationExTexinfo;
            _map[".tsv"] = MediaType.TextTabSeparatedValues;
            _map[".ustar"] = MediaType.ApplicationExUstar;
            _map[".uls"] = MediaType.TextIuls;
            _map[".vcf"] = MediaType.TextExVcard;
            _map[".wps"] = MediaType.ApplicationVndMsWorks;
            _map[".wav"] = MediaType.AudioWav;
            _map[".wrz"] = MediaType.ExWorldExVrml;
            _map[".wri"] = MediaType.ApplicationExMswrite;
            _map[".wks"] = MediaType.ApplicationVndMsWorks;
            _map[".wmf"] = MediaType.ApplicationExMsmetafile;
            _map[".wcm"] = MediaType.ApplicationVndMsWorks;
            _map[".wrl"] = MediaType.ExWorldExVrml;
            _map[".wdb"] = MediaType.ApplicationVndMsWorks;
            _map[".wsdl"] = MediaType.TextXml;
            _map[".xaml"] = MediaType.ApplicationXamlXml;
            _map[".xap"] = MediaType.ApplicationExSilverlightApp;
            _map[".xml"] = MediaType.TextXml;
            _map[".xlm"] = MediaType.ApplicationVndMsExcel;
            _map[".xaf"] = MediaType.ExWorldExVrml;
            _map[".xbap"] = MediaType.ApplicationExMsXbap;
            _map[".xla"] = MediaType.ApplicationVndMsExcel;
            _map[".xls"] = MediaType.ApplicationVndMsExcel;
            _map[".xof"] = MediaType.ExWorldExVrml;
            _map[".xlt"] = MediaType.ApplicationVndMsExcel;
            _map[".xlc"] = MediaType.ApplicationVndMsExcel;
            _map[".xsl"] = MediaType.TextXml;
            _map[".xbm"] = MediaType.ImageExXbitmap;
            _map[".xlw"] = MediaType.ApplicationVndMsExcel;
            _map[".xps"] = MediaType.ApplicationVndMsXpsdocument;
            _map[".xpm"] = MediaType.ImageExXpixmap;
            _map[".xwd"] = MediaType.ImageExXwindowdump;
            _map[".xsd"] = MediaType.TextXml;
            _map[".z"] = MediaType.ApplicationExCompress;
            _map[".zip"] = MediaType.ApplicationExZipCompressed;
            _map[".swf"] = MediaType.ApplicationExShockwaveFlash;
            _map[".flv"] = MediaType.VideoExFlv;
            _map[".flv2"] = MediaType.FlvApplicationOctetStream;
            _map[".apk"] = MediaType.ApplicationVndAndroidPackageArchive;
            _map[".*"] = MediaType.ApplicationOctetStream;
        }


      
        public static string Get(string filename)
        {
            string str = null;
            int startIndex = filename.LastIndexOf('.');
            if ((0 < startIndex) && (startIndex > filename.LastIndexOf('\\')))
            {
                str = _map.GetIfExists(filename.Substring(startIndex));
            }
            if (str == null)
            {
                str = _map[".*"];
            }
            return str;
        }

        public static void Set(string extension, string mimeType)
        {
            _map[extension] = mimeType;
        }

        public static IEnumerable<string> List()
        {
            return Fn.Distinct(_map.Values);
        }



    }

}
